const app = getApp()

Page({
  data: {
    cityList: [
    //   { city: '杭州市', children: [{ countryName: '西湖区', state: 0 }, { countryName: '滨江区', state: 0 }, { countryName: '拱墅区', state: 0 }, { countryName: '余杭区', state: 0 }, { countryName: '四个字区', state: 0 }, { countryName: '西湖区', state: 0 }, { countryName: '四个字区', state: 0 }, { countryName: '拱墅区', state: 0 }, { countryName: '余杭区', state: 0 }, { countryName: '四个字区', state: 0 }, { countryName: '余杭区', state: 0 }, { countryName: '西湖区', state: 0  }] },
    //   { city: '宁波市', children: [{ countryName: '西湖区', state: 0 }, { countryName: '滨江区', state: 0 }, { countryName: '拱墅区', state: 0 }, { countryName: '余杭区', state: 0 }, { countryName: '四个字区', state: 0 }, { countryName: '西湖区', state: 0 }, { countryName: '四个字区', state: 0 }, { countryName: '拱墅区', state: 0 }, { countryName: '余杭区', state: 0 }, { countryName: '四个字区', state: 0 }, { countryName: '余杭区', state: 0 }, { countryName: '西湖区', state: 0 }] },
    //   { city: '温州市', children: [{ countryName: '西湖区', state: 0 }, { countryName: '滨江区', state: 0 }, { countryName: '拱墅区', state: 0 }, { countryName: '余杭区', state: 0 }, { countryName: '四个字区', state: 0 }, { countryName: '西湖区', state: 0 }, { countryName: '四个字区', state: 0 }, { countryName: '拱墅区', state: 0 }, { countryName: '余杭区', state: 0 }, { countryName: '四个字区', state: 0 }, { countryName: '余杭区', state: 0 }, { countryName: '西湖区', state: 0 }] },
    //   { city: '绍兴市', children: [{ countryName: '西湖区', state: 0 }, { countryName: '滨江区', state: 0 }, { countryName: '拱墅区', state: 0 }, { countryName: '余杭区', state: 0 }, { countryName: '四个字区', state: 0 }, { countryName: '西湖区', state: 0 }, { countryName: '四个字区', state: 0 }, { countryName: '拱墅区', state: 0 }, { countryName: '余杭区', state: 0 }, { countryName: '四个字区', state: 0 }, { countryName: '余杭区', state: 0 }, { countryName: '西湖区', state: 0 }] },
    ],
    lastChoosed: null,
    choosedName: ''
  },
  onLoad: function () {
    wx.setNavigationBarTitle({
      title: '城市选择',
    });
    this.initPage();
  },
  initPage(){
    var _this = this;
    app.doReq(app.globalData.SERVICE.INDEX.ADD,
      {},'get',
      function (res) {
        let list= res.data.obj.child;
        for(let i of list){
          i.state=0;
        }   
        _this.setData({
        cityList :list
        })
      });
  },
  choosedWitchCountry: function (e) {
    wx.setStorageSync('addName', e.currentTarget.dataset.city + e.currentTarget.dataset.country);
    wx.setStorageSync('addCode', e.currentTarget.dataset.countrycode);
    var pages = getCurrentPages();
    var currentPage = pages[pages.length - 1];//当前页面
    var pervPagge = pages[pages.length - 2];//上个页面
    pervPagge.setData({
      addName: e.currentTarget.dataset.city + e.currentTarget.dataset.country,
      addCode: e.currentTarget.dataset.countrycode,
    })
    wx.navigateBack({});

    
    // var _this = this;
    // var lastChooseds = _this.data.lastChoosed;
    // var thisChooseds = "cityList[" + e.currentTarget.dataset.cityindex + "].children[" + e.currentTarget.dataset.countryindex +"].state";
    // if (lastChooseds != thisChooseds) {
    //   _this.setData({
    //     [thisChooseds]: 1,
    //     [lastChooseds]: 0,
    //     lastChoosed: thisChooseds,
    //     choosedName: e.currentTarget.dataset.city + e.currentTarget.dataset.country
    //   })
    // }
  },
  choosedWitchCity: function (e) {
    // 选中哪一个市
    var _this = this;
    var lastChooseds = _this.data.lastChoosed;
    _this.setData({
      [lastChooseds]: 0,
      choosedName: e.currentTarget.dataset.city
    })
    wx.setStorageSync('addName', e.currentTarget.dataset.city );
    wx.setStorageSync('addCode', e.currentTarget.dataset.citycode);
    var pages = getCurrentPages();
    var currentPage = pages[pages.length - 1];//当前页面
    var pervPagge = pages[pages.length - 2];//上个页面
    pervPagge.setData({
      addName: e.currentTarget.dataset.city,
      addCode: e.currentTarget.dataset.countrycode,
    })
    wx.navigateBack({});
  },
  choosedProvince: function (e) {
    // 选中省
    var _this = this;
    wx.setStorageSync('addName', e.currentTarget.dataset.pro);
    wx.setStorageSync('addCode', e.currentTarget.dataset.procode);
    var pages = getCurrentPages();
    var currentPage = pages[pages.length - 1];//当前页面
    var pervPagge = pages[pages.length - 2];//上个页面
    pervPagge.setData({
      addName: e.currentTarget.dataset.pro,
      addCode: e.currentTarget.dataset.procode
    })
    wx.navigateBack({});
  }
})